package com.yztc.thinkpad.mandemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yztc.thinkpad.mandemo.coolconstant.Constant;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/9.
 *
 * 投稿activity
 */
public class ContributionActivity extends AppCompatActivity {
    @Bind(R.id.cool_contribution_toolbar)
    Toolbar coolContributionToolbar;
    @Bind(R.id.cool_contribution_webview)
    WebView coolContributionWebview;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cool_contribution);
        ButterKnife.bind(this);
        initview();
        initdata();
    }
        //加载网络数据
    private void initdata() {

        //设置webiew的背景颜色
        coolContributionWebview.setBackgroundColor(Color.WHITE);
        //设置字符集编码
        coolContributionWebview.getSettings().setDefaultTextEncodingName("utf-8");
        //设置webview的缩放功能  设置webview触摸放大缩小
        coolContributionWebview.getSettings().setSupportZoom(true);
        coolContributionWebview.getSettings().setBuiltInZoomControls(true);
        //设置webview中滚动条的样式  SCROLLBARS_OUTSIDE_INSET  SCROLLBARS_OUTSIDE_OVERLAY
        //SCROLLBARS_INSIDE_INSET SCROLLBARS_INSIDE_OVERLAY
        coolContributionWebview.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        coolContributionWebview.setHorizontalScrollBarEnabled(false);//禁止水平滚动条

        //设置当webview双击时变大 再双击就变小  当手动放大时  双击可以恢复到原始大小
        coolContributionWebview.getSettings().setUseWideViewPort(true);
        //设置webview的初始化尺寸  参数百分比
        coolContributionWebview.setInitialScale(100);
        //点击网页中的超链接时不弹出浏览器的窗口 在webview控件中加载url
        coolContributionWebview.setWebViewClient(new WebViewClient());
        coolContributionWebview.loadUrl(Constant.CONTRIBUTION);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(coolContributionWebview.canGoBack()){
                coolContributionWebview.goBack();//goBack() 表示返回webview中的上一页面
            }else{
                finish();
            }
        }
        return true;
    }
        //初始化控件
    private void initview() {
        coolContributionToolbar.setNavigationIcon(R.mipmap.back_left_white);//设置导航栏图标
        coolContributionToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
